package com.moxi.mogublog.web.restapi.xuan;


import com.moxi.cloudblog.base.enums.EBehavior;
import com.moxi.cloudblog.base.exception.ThrowableUtils;
import com.moxi.cloudblog.base.holder.RequestHolder;
import com.moxi.mogublog.commons.feign.SpiderFeignClient;
import com.moxi.mogublog.utils.IpUtils;
import com.moxi.mogublog.utils.ResultUtil;
import com.moxi.mogublog.web.annotion.log.BussinessLog;
import com.moxi.mogublog.commons.vo.college.CollegeSortVO;
import com.moxi.cloudblog.base.validator.group.GetList;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;

@Api(value = "大学博客相关接口", tags = {"大学博客相关接口"})
@RestController
@RequestMapping("/college")
@Slf4j
public class CollegeRestApi {

    @Resource
    private SpiderFeignClient spiderFeignClient;

    @Autowired
    private StringRedisTemplate redisTemplate;


    @ApiOperation(value = "获取大学列表", notes = "获取大学列表", response = String.class)
    @PostMapping("/getList")
    public String getCollegeList(@Validated({GetList.class}) @RequestBody CollegeSortVO collegeSortVO, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        return spiderFeignClient.getCollegeList(collegeSortVO);
    }


    // @BussinessLog(value = "点击分类", behavior = EBehavior.VISIT_CLASSIFY)       //要研究一下这个的作用是什么
    @ApiOperation(value = "通过collegeSortUid获取文章", notes = "通过collegeSortUid获取文章")
    @GetMapping("/getArticleByCollegeSortUid")
    public String getArticleByCollegeSortUid(@ApiParam(name = "collegeSortUid", value = "分类UID", required = false) @RequestParam(name = "collegeSortUid", required = false) String collegeSortUid,
                                             @ApiParam(name = "currentPage", value = "当前页数", required = false) @RequestParam(name = "currentPage", required = false, defaultValue = "1") Long currentPage,
                                             @ApiParam(name = "pageSize", value = "每页显示数目", required = false) @RequestParam(name = "pageSize", required = false, defaultValue = "10") Long pageSize) {
        log.info("通过collegeSortUid获取文章，collegeSortUid：{}", collegeSortUid);
        return spiderFeignClient.getArticleByCollegeSortUid(collegeSortUid, currentPage, pageSize);
    }


    @BussinessLog(value = "点击校园博客", behavior = EBehavior.BLOG_CONTNET)
    @ApiOperation(value = "通过Uid获取校园博客内容", notes = "通过Uid获取校园博客内容")
    @GetMapping("/getBlogByUid")
    public String getBlogByUid(@ApiParam(name = "uid", value = "博客UID", required = false) @RequestParam(name = "uid", required = false) String uid,
                               @ApiParam(name = "oid", value = "博客OID", required = false) @RequestParam(name = "oid", required = false, defaultValue = "0") Integer oid) {
        HttpServletRequest request = RequestHolder.getRequest();
        String ip = IpUtils.getIpAddr(request);
        return spiderFeignClient.getBlogByUid(uid, oid, ip);
    }

    @ApiOperation(value = "获取大学Hot列表", notes = "获取大学Hot列表", response = String.class)
    @GetMapping("/getHotList")
    public String getHotCollegeList() {
        return spiderFeignClient.getHotCollegeList();
    }

    @ApiOperation(value = "增加college点击数", notes = "增加college点击数")
    @GetMapping("/addClick")
    public String addClick(@ApiParam(name = "cid", value = "校园CID", required = true)
                           @RequestParam(name = "cid", required = true) String cid,
                           HttpServletRequest request) {
        String ipAddress = request.getRemoteAddr();
        String key = "collegeAddClick:" + ipAddress + ":" + cid;
        if (redisTemplate.hasKey(key)) {
            return ResultUtil.errorWithData("");
        }

        String response = spiderFeignClient.addClick(cid);

        redisTemplate.opsForValue().set(key, "clicked", 1, TimeUnit.DAYS);
        return response;
    }

    @ApiOperation(value = "获取Hot大学Topic", notes = "获取Hot大学Topic", response = String.class)
    @GetMapping("/getHotTopic")
    public String getHotTopic() {
        return spiderFeignClient.getHotTopic();
    }

}
